import numpy as np
import matplotlib.pyplot as plt
import time
from bid_functions import InterceptionPro, bid, bidding, auction

def TargetAssign(info_m, info_t):
    # 输入：info_m，info_t：我方/敌方信息 [x, y, z, 航向角, 探测范围, 速度]
    # 输出：result_fenpei:顺序对应我方分配的敌方序号
    # 输入示例（6v6）：
    # info_m = np.array([
    #     [7, 29.5, 20, -15, 100, 1100],
    #     [8, 27,   20, -25, 100, 1100],
    #     [2, 22.5, 20, -15, 100, 1100],
    #     [3, 19,   20,  25, 100, 1100],
    #     [6, 12.5, 20, -10, 100, 1100],
    #     [4, 9,    20,  10, 100, 1100]
    # ])
    # 目标信息 [x, y, z, 重要程度, 速度, 航向角]
    # info_t = np.array([
    #     [17.5, 26,   20, 1, 500, 170],
    #     [16.5, 22.5, 20, 1, 500, 195],
    #     [17.5, 20,   20, 1, 500, 125],
    #     [15.5, 17,   20, 1, 500, 170],
    #     [18.5, 12.5, 20, 1, 500, 182],
    #     [19.5, 9.5,  20, 1, 500, 160]
    # ])
    #输出示例：result_fenpei = [2,4,3,6,5,1]

    nm = info_m.shape[0]  # 导弹个数
    nt = info_t.shape[0]  # 目标个数
    num = 1               # 分配次数
    result = np.zeros((num, nm))  # 分配结果矩阵
    f_gen = np.zeros(num)         # 价值结果向量

    tdead = 0
    start_time = time.time()  # 开始计时
    seed = 2
    np.random.seed(seed)  # 设置随机种子
    x1 = np.array([])
    y1 = np.array([])

    for gen in range(num):
        # 随机排列导弹索引
        # r = np.random.permutation(info_m.shape[0])
        r = range(6)
        info_m1 = info_m[r, :]  # 随机排列后的导弹信息
        
        # 调用拦截概率计算函数
        P = InterceptionPro(info_m1, info_t)
        
        # 调用出价计算函数
        F = bid(nm, nt, P, info_t)
        
        # 拍卖竞标
        epsilon = 0.1
        # 调用拍卖分配函数
        H, X, f, x1, y1 = bidding(nt, nm, F, epsilon, x1, y1)
        
        f_gen[gen] = f  # 记录总价值
        
        # 将分配结果映射回原始导弹顺序
        for i in range(nm):
            result[gen, r[i]] = H[i]

    end_time = time.time()  # 结束计时
    tt = end_time - start_time

    # region 绘制拍卖结果图
    # 设置中文字体
    # plt.rcParams['font.sans-serif'] = ['SimHei']  # Windows系统
    # plt.rcParams['axes.unicode_minus'] = False  # 解决负号显示问题
    # plt.plot(x1, y1)
    # plt.xlabel('拍卖轮数')
    # plt.ylabel('目标函数值')
    # plt.title('拍卖结果图')
    # plt.grid(True)
    # plt.show()
    # endregion

    # 对结果进行排序
    sorted_indices = np.argsort(f_gen)[::-1]  # 降序排列索引
    f_gen_sorted = f_gen[sorted_indices]
    value = f_gen_sorted[0]
    result2 = result[sorted_indices[0], :]
    result_fenpei = list(int(float(x)) for x in result2)   # 转换为int
    # print(f"最优值: {value}")
    # print(f"分配结果: {result_fenpei}")
    # print(f"运行时间: {tt:.3f} s")
    return result_fenpei


# region  function_test
# 导弹信息 [x, y, z, 航向角, 探测范围, 速度]
info_m = np.array([
    [7, 29.5, 20, -15, 100, 1100],
    [8, 27,   20, -25, 100, 1100],
    [2, 22.5, 20, -15, 100, 1100],
    [3, 19,   20,  25, 100, 1100],
    [6, 12.5, 20, -10, 100, 1100],
    [4, 9,    20,  10, 100, 1100]
])
# 目标信息 [x, y, z, 重要程度, 速度, 航向角]
info_t = np.array([
    [17.5, 26,   20, 1, 500, 170],
    [16.5, 22.5, 20, 1, 500, 195],
    [17.5, 20,   20, 1, 500, 125],
    [15.5, 17,   20, 1, 500, 170],
    [18.5, 12.5, 20, 1, 500, 182],
    [19.5, 9.5,  20, 1, 500, 160]
])
result_fenpei = TargetAssign(info_m, info_t)
# endregion
# print(f"分配结果: {result_fenpei}")